#ifndef __SERVICE_H_
#define __SERVICE_H_

#include <muduo/net/TcpConnection.h>
#include <unordered_map>
#include <functional>
#include <mutex>
using namespace std;
using namespace muduo;
using namespace muduo::net;

#include "usermodel.h"
#include "friendmodel.h"
#include "json.hpp"
#include "redis.h"
using json = nlohmann::json;

using MsgHandler = std::function<void(const TcpConnectionPtr &conn, json &js, Timestamp)>;

//业务类
class Service 
{
public:
    static Service* instance();

    //处理登录业务
    void login(const TcpConnectionPtr &conn, json &js, Timestamp time);
    //处理注册业务
    void reg(const TcpConnectionPtr &conn, json &js, Timestamp time);
    //处理聊天业务
    void chat(const TcpConnectionPtr &conn, json &js, Timestamp time);

    MsgHandler getHandler(int msgid);
    //客户端异常退出
    void clientCloseException(const TcpConnectionPtr &conn);

    // 从redis消息队列中获取订阅的消息
    void handleRedisSubscribeMessage(string strAccount, string);
private:
    Service();
    unordered_map<int, MsgHandler> msgHandlerMap;

    //记录在线用户的连接（考虑线程安全）
    unordered_map<string, TcpConnectionPtr> userConnectMap;
    mutex connectMutex;

    UserModel userModel;
    FriendModel friendModel;

    // redis操作对象
    Redis _redis;
};

#endif